package com.moshme.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.moshme.common.core.domain.AjaxResult;
import com.moshme.common.exception.ServiceException;
import com.moshme.project.domain.*;
import com.moshme.project.domain.dto.StageSurvey;
import com.moshme.project.mapper.*;
import com.moshme.project.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 19803
 * @description 勘察阶段服务类实现类
 * @createDate 2023-09-05 09:09:44
 */
@Service
public class StageSurveyServiceImpl implements StageSurveyService {

    @Autowired
    private StageSurveyInfoService stageSurveyInfoService;

    @Resource
    private StageSurveyInfoMapper stageSurveyInfoMapper;

    @Autowired
    private StageSurveyPeripheralMppnService stageSurveyPeripheralMppnService;

    @Resource
    private StageSurveyPeripheralMppnMapper stageSurveyPeripheralMppnMapper;

    @Autowired
    private StageSurveyGasMcvService stageSurveyGasMcvService;

    @Resource
    private StageSurveyGasMcvMapper stageSurveyGasMcvMapper;

    @Autowired
    private StageSurveyInnerMppnService stageSurveyInnerMppnService;

    @Resource
    private StageSurveyInnerMppnMapper stageSurveyInnerMppnMapper;

    @Autowired
    private StageSurveyPrfService stageSurveyPrfService;

    @Resource
    private StageSurveyPrfMapper stageSurveyPrfMapper;

    @Autowired
    private StageSurveyInnerLppnService stageSurveyInnerLppnService;

    @Resource
    private StageSurveyInnerLppnMapper stageSurveyInnerLppnMapper;

    @Autowired
    private StageSurveyConfinedSpaceService stageSurveyConfinedSpaceService;

    @Resource
    private StageSurveyConfinedSpaceMapper stageSurveyConfinedSpaceMapper;

    @Autowired
    private FileService fileService;

    /**
     * 根据项目id获取勘察阶段
     *
     * @param projectId
     */
    @Override
    public StageSurvey get(Long projectId) {
        StageSurvey stageSurvey = new StageSurvey();
        LambdaQueryWrapper<StageSurveyInfo> stageSurveyInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        StageSurveyInfo stageSurveyInfo = stageSurveyInfoService.getOne(stageSurveyInfoLambdaQueryWrapper.eq(StageSurveyInfo::getProjectId, projectId));
        stageSurvey.setStageSurveyInfo(stageSurveyInfo);
        stageSurvey.setStageSurveyPeripheralMppnList(stageSurveyPeripheralMppnService.getListByProject(projectId));
        stageSurvey.setStageSurveyGasMcvList(stageSurveyGasMcvService.getListByProject(projectId));
        stageSurvey.setStageSurveyInnerMppnList(stageSurveyInnerMppnService.getListByProject(projectId));
        stageSurvey.setStageSurveyPrfList(stageSurveyPrfService.getListByProject(projectId));
        stageSurvey.setStageSurveyInnerLppnList(stageSurveyInnerLppnService.getListByProject(projectId));
        stageSurvey.setStageSurveyConfinedSpaceList(stageSurveyConfinedSpaceService.getListByProject(projectId));
        return stageSurvey;
    }

    /**
     * 根据项目id创建勘察阶段
     *
     * @param projectId
     */
    @Override

    @Transactional(rollbackFor = Exception.class)
    public boolean add(Long projectId) {
        LambdaQueryWrapper<StageSurveyInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        StageSurveyInfo stageSurveyInfo = stageSurveyInfoService.getOne(lambdaQueryWrapper.eq(StageSurveyInfo::getProjectId, projectId));
        if (stageSurveyInfo != null) {
            throw new ServiceException("勘察阶段已存在!");
        }
        stageSurveyInfo = new StageSurveyInfo();
        stageSurveyInfo.setProjectId(projectId);
        stageSurveyInfoService.save(stageSurveyInfo);
        StageSurveyPeripheralMppn stageSurveyPeripheralMppn = new StageSurveyPeripheralMppn();
        stageSurveyPeripheralMppn.setProjectId(projectId);
        stageSurveyPeripheralMppnService.save(stageSurveyPeripheralMppn);
        StageSurveyGasMcv stageSurveyGasMcv = new StageSurveyGasMcv();
        stageSurveyGasMcv.setProjectId(projectId);
        stageSurveyGasMcvService.save(stageSurveyGasMcv);
        StageSurveyInnerMppn stageSurveyInnerMppn = new StageSurveyInnerMppn();
        stageSurveyInnerMppn.setProjectId(projectId);
        stageSurveyInnerMppnService.save(stageSurveyInnerMppn);
        StageSurveyPrf stageSurveyPrf = new StageSurveyPrf();
        stageSurveyPrf.setProjectId(projectId);
        stageSurveyPrfService.save(stageSurveyPrf);
        StageSurveyInnerLppn stageSurveyInnerLppn = new StageSurveyInnerLppn();
        stageSurveyInnerLppn.setProjectId(projectId);
        stageSurveyInnerLppnService.save(stageSurveyInnerLppn);
        StageSurveyConfinedSpace stageSurveyConfinedSpace = new StageSurveyConfinedSpace();
        stageSurveyConfinedSpace.setProjectId(projectId);
        stageSurveyConfinedSpaceService.save(stageSurveyConfinedSpace);
        return true;
    }

    /**
     * 项目更新勘察阶段
     *
     * @param stageSurvey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(StageSurvey stageSurvey) {
        if (stageSurvey.getStageSurveyInfo() != null) {
            if (!stageSurveyInfoService.updateById(stageSurvey.getStageSurveyInfo())) {
                return false;
            }
        }
        if (stageSurvey.getStageSurveyPeripheralMppnList() != null) {
            stageSurvey.getStageSurveyPeripheralMppnList().stream().forEach(s -> {
                        if (s.getId() == null) {
                            throw new ServiceException("修改失败");
                        }
                    }
            );
            if (!stageSurveyPeripheralMppnService.updateBatchById(stageSurvey.getStageSurveyPeripheralMppnList())) {
                return false;
            }
        }
        if (stageSurvey.getStageSurveyGasMcvList() != null) {
            stageSurvey.getStageSurveyGasMcvList().stream().forEach(s -> {
                        if (s.getId() == null) {
                            throw new ServiceException("修改失败");
                        }
                    }
            );
            if (!stageSurveyGasMcvService.updateBatchById(stageSurvey.getStageSurveyGasMcvList())) {
                return false;
            }
        }
        if (stageSurvey.getStageSurveyInnerMppnList() != null) {
            stageSurvey.getStageSurveyInnerMppnList().stream().forEach(s -> {
                        if (s.getId() == null) {
                            throw new ServiceException("修改失败");
                        }
                    }
            );
            if (!stageSurveyInnerMppnService.updateBatchById(stageSurvey.getStageSurveyInnerMppnList())) {
                return false;
            }
        }
        if (stageSurvey.getStageSurveyPrfList() != null) {
            stageSurvey.getStageSurveyPrfList().stream().forEach(s -> {
                        if (s.getId() == null) {
                            throw new ServiceException("修改失败");
                        }
                    }
            );
            if (!stageSurveyPrfService.updateBatchById(stageSurvey.getStageSurveyPrfList())) {
                return false;
            }
        }
        if (stageSurvey.getStageSurveyInnerLppnList() != null) {
            stageSurvey.getStageSurveyInnerLppnList().stream().forEach(s -> {
                        if (s.getId() == null) {
                            throw new ServiceException("修改失败");
                        }
                    }
            );
            if (!stageSurveyInnerLppnService.updateBatchById(stageSurvey.getStageSurveyInnerLppnList())) {
                return false;
            }
        }
        if (stageSurvey.getStageSurveyConfinedSpaceList() != null) {
            stageSurvey.getStageSurveyConfinedSpaceList().stream().forEach(s -> {
                        if (s.getId() == null) {
                            throw new ServiceException("修改失败");
                        }
                    }
            );
            if (!stageSurveyConfinedSpaceService.updateBatchById(stageSurvey.getStageSurveyConfinedSpaceList())) {
                return false;
            }
        }
        return true;
    }

    /**
     * 根据项目id删除勘察阶段
     *
     * @param projectId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean del(Long projectId) {
        LambdaUpdateWrapper<StageSurveyInfo> stageSurveyInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        stageSurveyInfoService.remove(stageSurveyInfoLambdaUpdateWrapper.eq(StageSurveyInfo::getProjectId, projectId));
        LambdaUpdateWrapper<StageSurveyPeripheralMppn> stageSurveyPeripheralMppnLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        stageSurveyPeripheralMppnService.remove(stageSurveyPeripheralMppnLambdaUpdateWrapper.eq(StageSurveyPeripheralMppn::getProjectId, projectId));
        LambdaUpdateWrapper<StageSurveyGasMcv> stageSurveyGasMcvLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        stageSurveyGasMcvService.remove(stageSurveyGasMcvLambdaUpdateWrapper.eq(StageSurveyGasMcv::getProjectId, projectId));
        LambdaUpdateWrapper<StageSurveyInnerMppn> stageSurveyInnerMppnLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        stageSurveyInnerMppnService.remove(stageSurveyInnerMppnLambdaUpdateWrapper.eq(StageSurveyInnerMppn::getProjectId, projectId));
        LambdaUpdateWrapper<StageSurveyPrf> stageSurveyPrfLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        stageSurveyPrfService.remove(stageSurveyPrfLambdaUpdateWrapper.eq(StageSurveyPrf::getProjectId, projectId));
        LambdaUpdateWrapper<StageSurveyInnerLppn> stageSurveyInnerLppnLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        stageSurveyInnerLppnService.remove(stageSurveyInnerLppnLambdaUpdateWrapper.eq(StageSurveyInnerLppn::getProjectId, projectId));
        LambdaUpdateWrapper<StageSurveyConfinedSpace> stageSurveyConfinedSpaceLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        stageSurveyConfinedSpaceService.remove(stageSurveyConfinedSpaceLambdaUpdateWrapper.eq(StageSurveyConfinedSpace::getProjectId, projectId));
        fileService.delprojectAllFiles(projectId);
        return true;
    }

    /**
     * 根据项目id列表批量删除勘察阶段
     *
     * @param projectIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDel(List<Long> projectIds) {
        stageSurveyInfoMapper.delByProjectIdIn(projectIds);
        stageSurveyPeripheralMppnMapper.delByProjectIdIn(projectIds);
        stageSurveyGasMcvMapper.delByProjectIdIn(projectIds);
        stageSurveyInnerMppnMapper.delByProjectIdIn(projectIds);
        stageSurveyPrfMapper.delByProjectIdIn(projectIds);
        stageSurveyInnerLppnMapper.delByProjectIdIn(projectIds);
        stageSurveyConfinedSpaceMapper.delByProjectIdIn(projectIds);
        for (Long projectId : projectIds) {
            fileService.delprojectAllFiles(projectId);
        }
        return true;
    }

}




